package com.yc.demohoudun.service;

import com.yc.demohoudun.entiy.Product;
import com.yc.demohoudun.entiy.Result;
import com.yc.demohoudun.entiy.Score;
import com.yc.demohoudun.mapper.ProductMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

import static com.yc.demohoudun.web.UserAction.usernewName;

@Service
public class ProductService {
    @Autowired
    ProductMapper productMapper;

    public Product  save(Product product) throws ServiceException {

        if(product.getName()==null || product.getName().isBlank()){
            throw new ServiceException("请输入商品名称");
        }
        if(product.getPrice()<=0){
            throw new ServiceException("请输入商品价格");
        }
        if(productMapper.countByName(product.getName())==true){
            throw new ServiceException("商品已录入");
        }
        if(product.getId()==0){
            productMapper.insert(product);
        }else {
            productMapper.inOwn(usernewName,product.getName());
            productMapper.updateById(product);
        }
return product;
    }

    public Result selectDetail(int id) {

        Product product=new Product();
        product=productMapper.selectById(id);
        return  new Result(1,"cdws",product);
    }

    public Result selectProductscore(int id) {

         List<Score>   scores=productMapper.selectByproductId(id);

        return new Result(1,"得分查询成功",scores);
    }

    public Result inscore(Score score1) {
             String product=productMapper.selectById(score1.getProductId()).getName();
             score1.setProduct(product);
             productMapper.insertsco(score1);


return new Result(1,"得分查询成功",score1);
    }

    public Result selectByname(String name) {

        Product product=new Product();
        product=productMapper.selectByname(name);
        return new Result(1,"查询成功",product);
    }
}
